<template>
  <div class="details">
    <van-swipe :autoplay="3000" indicator-color="skyblue">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" width="100%" style="display: block" alt="" />
      </van-swipe-item>
    </van-swipe>
    <Tips />
    <div class="goods_info">
      <h3>{{ info.name }}</h3>
      <p>{{ info.goods_brief }}</p>
      <section>{{ info.retail_price | filterMoney }}</section>
    </div>
    <van-cell is-link @click="showPopup">展示弹出层</van-cell>
    <div class="good_canshu">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <span>{{ item.name }}</span>
          <p>{{ item.value }}</p>
        </li>
      </ul>
    </div>
    <div class="goods_desc" v-html="info.goods_desc"></div>
    <div class="title">
      <div class="line"></div>
      <h2>常见问题</h2>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <div class="title">
      <div class="line"></div>
      <h2>相关产品</h2>
    </div>
    <div class="mychanpin">
      <Chanpin :goodsList="goodsList" />
    </div>
    <van-popup v-model="popupShow" position="bottom" :style="{ height: '30%' }">
      <div class="sku">
        <div class="top">
          <img :src="info.list_pic_url" alt="" />
          <div class="top_right">
            <section>价格：{{ info.retail_price | filterMoney }}</section>
            <section>库存：{{ info.goods_number }}</section>
          </div>
        </div>
        <div class="down">
          <h3>数量：</h3>
          <van-stepper
            v-model="stepNum"
            integer
            min="1"
            :max="info.goods_number"
          />
        </div>
      </div>
    </van-popup>
    <van-goods-action>
      <van-goods-action-icon icon="star-o" text="收藏" />
      <van-goods-action-icon icon="cart-o" text="购物车" @click="gotoCart" :badge="goodsCount" />
      <van-goods-action-button type="danger" text="立即购买" />
      <van-goods-action-button
        type="warning"
        text="加入购物车"
        @click="addToCartFn"
      />
    </van-goods-action>
  </div>
</template>

<script>
import Chanpin from "@/components/Chanpin.vue";
import Tips from "@/components/Tips.vue";
import {
  GoodsDetailApi,
  GoodsAboutApi,
  CartGoodsNumApi,
  CartAddApi,
} from "@/request/api";
export default {
  async created() {
    // 请求商品数据
    await GoodsDetailApi({
      id: this.$route.params.id,
    }).then((res) => {
      if (res.errno === 0) {
        let { gallery, info, attribute, issue, productList } = res.data;
        this.gallery = gallery;
        this.info = info;
        this.attribute = attribute;
        this.issue = issue;
        this.productList = productList;
      }
    });

    // 获取购物车产品数量
    await CartGoodsNumApi().then((res) => {
      if (res.errno === 0) {
        let { cartTotal } = res.data;
        this.goodsCount = cartTotal.goodsCount;
      }
    });

    // 请求相关产品
    await GoodsAboutApi({
      id: this.$route.params.id,
    }).then((res) => {
      if (res.errno === 0) {
        let { goodsList } = res.data;
        this.goodsList = goodsList;
      }
    });
  },
  data() {
    return {
      popupShow: false, // 默认关闭弹出层
      gallery: [], // 轮播图
      info: {}, // 商品信息
      attribute: [], // 商品参数
      issue: [], // 常见问题
      goodsList: [], // 相关产品的列表
      stepNum: 1, // 步进器的值
      goodsCount: 0, // 购物车数量
      productList: [],
    };
  },
  components: { Tips, Chanpin },
  methods: {
    // 进入购物车页面
    gotoCart(){
      this.$router.push('/cart')
    },
    // 显示popup
    showPopup() {
      this.popupShow = true;
    },
    // 点击加入购物车
    addToCartFn() {
      // 判断popup到底有没有打开，如果没打开，就先打开popup
      if (this.popupShow) {
        // 已经打开，等会该干啥就干啥
        CartAddApi({
          goodsId: this.$route.params.id,
          productId: this.productList[0].id,
          number: this.stepNum,
        }).then((res) => {
          if (res.errno === 0) {
            this.goodsCount = res.data.cartTotal.goodsCount; // 重新渲染购物车数量
            this.popupShow = false; // 关闭遮罩层
          }
        });
      } else {
        this.popupShow = true;
      }
    },
  },
  // 监听路由的变化
  watch: {
    "$route.params.id": {
      handler(val, old) {
        if (val !== old) {
          this.$router.go(0); // 刷新当前页
        }
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
.details {
  padding-bottom: 60px;
  .goods_info {
    background: #fff;
    padding: 20px 0;
    text-align: center;
    border-bottom: 1px solid #ccc;
    h3 {
      font-size: 18px;
    }
    p {
      margin-top: 20px;
      margin-bottom: 20px;
    }
    section {
      color: darkred;
    }
  }

  .good_canshu {
    font-size: 18px;
    background: #fff;
    padding: 20px;
    ul {
      margin-top: 10px;
      font-size: 14px;
      li {
        border-radius: 6px;
        background: #efefef;
        margin-bottom: 10px;
        display: flex;
        height: 30px;
        justify-content: space-between;
        align-items: center;
        span {
          width: 20%;
          text-align: right;
          color: #999;
          margin-right: 10px;
        }
        p {
          flex: 1;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
      }
    }
  }

  /* less和sass - 预编译语言 - deep深度修改样式 */
  /deep/.goods_desc {
    width: 100%;
    margin-bottom: 20px;
    p {
      margin: 0;
      img {
        display: block;
        width: 100%;
      }
    }
  }

  .title {
    height: 40px;
    position: relative;
    background: #fff;
    .line {
      width: 50%;
      background: #ccc;
      position: absolute;
      height: 2px;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }
    h2 {
      width: 30%;
      background: #fff;
      text-align: center;
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      font-size: 16px;
      line-height: 40px;
      margin: auto;
    }
  }

  .issue {
    background: #fff;
    padding: 10px 20px;
    li {
      h3 {
        font-size: 14px;
        position: relative;
        &::before {
          content: "";
          width: 4px;
          height: 4px;
          border-radius: 50%;
          background: darkred;
          position: absolute;
          left: -10px;
          top: 50%;
          margin-top: -2px;
        }
      }
      p {
        font-size: 12px;
        color: #999;
      }
    }
  }

  .mychanpin {
    background: #fff;
  }

  .van-goods-action {
    z-index: 9999;
  }

  .sku {
    padding: 20px;
    .top {
      display: flex;
      justify-content: space-between;
      img {
        width: 100px;
        margin-right: 10px;
      }
      .top_right {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
      }
    }
  }
}
</style>